/**
 * Copyright (c) 2015-2018, Golly Hu 胡高 (3722711@qq.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.hg.jfsa.core;

import cn.hg.base.common.BaseConstants;

import javax.servlet.http.HttpSession;

/**
 * @author Golly
 *
 */
public class SessionThreadLocal {
	private static ThreadLocal<HttpSession> sessionTL = new ThreadLocal<>();

	public static void set(HttpSession hs) {
		sessionTL.set(hs);
	}

	public static HttpSession get() {
		return sessionTL.get();
	}

	@SuppressWarnings("unchecked")
	public static <T> T getAdmin() {
		if (null == get()) {
			return null;
		}

		return (T) get().getAttribute(BaseConstants.CURRENT_ADMIN);
	}

	@SuppressWarnings("unchecked")
	public static <T> T getAdminAuthAction() {
		if (null == get()) {
			return null;
		}
		return (T) get().getAttribute(BaseConstants.ADMIN_AUTH_ACTION);
	}

	@SuppressWarnings("unchecked")
	public static <T> T getAdminAuthMenu() {
		if (null == get()) {
			return null;
		}
		return (T) get().getAttribute(BaseConstants.ADMIN_AUTH_MENU);
	}

	@SuppressWarnings("unchecked")
	public static <T> T getUser() {
		if (null == get()) {
			return null;
		}

		return (T) get().getAttribute(BaseConstants.CURRENT_USER);
	}

	@SuppressWarnings("unchecked")
	public static <T> T getUserAuthAction() {
		if (null == get()) {
			return null;
		}
		return (T) get().getAttribute(BaseConstants.USER_AUTH_ACTION);
	}

	@SuppressWarnings("unchecked")
	public static <T> T getUserAuthMenu() {
		if (null == get()) {
			return null;
		}
		return (T) get().getAttribute(BaseConstants.USER_AUTH_MENU);
	}

	public static void setAdmin(Object admin) {
		if (null == get()) {
			return;
		}

		get().setAttribute(BaseConstants.CURRENT_ADMIN, admin);
	}

	public static void setAdminAuthAction(Object action) {
		if (null == get()) {
			return;
		}

		get().setAttribute(BaseConstants.ADMIN_AUTH_ACTION, action);
	}

	public static void setAdminAuthMenu(Object menu) {
		if (null == get()) {
			return;
		}

		get().setAttribute(BaseConstants.ADMIN_AUTH_MENU, menu);
	}

	public static void setUser(Object user) {
		if (null == get()) {
			return;
		}

		get().setAttribute(BaseConstants.CURRENT_USER, user);
	}

	public static void setUserAuthAction(Object action) {
		if (null == get()) {
			return;
		}

		get().setAttribute(BaseConstants.USER_AUTH_ACTION, action);
	}

	public static void setUserAuthMenu(Object menu) {
		if (null == get()) {
			return;
		}

		get().setAttribute(BaseConstants.USER_AUTH_MENU, menu);
	}

}
